package com.shopping.search.controller;

import com.cloud.shopping.search.feign.SkuSearchFeign;
import com.cloud.shopping.tool.RespResult;
import com.cloud.shopping.tool.UrlUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: SearchController, v 1.0 2021/1/2 15:25 ZhiYong Exp $
 */

@Controller
public class SearchController {

    @Resource
    private SkuSearchFeign skuSearchFeign;

    @GetMapping(value = "/web/search")
    public String search(@RequestParam(required = false) Map<String, Object> searchMap, Model model) {
        //搜索
        RespResult<Map<String, Object>> resultMap = skuSearchFeign.search(searchMap);

        //组装用户访问的url
        model.addAttribute("url", UrlUtils.map2url("/web/search", searchMap, "page"));
        model.addAttribute("urlSort", UrlUtils.map2url("/web/search", searchMap, "sm", "sfield", "page"));
        model.addAttribute("result", resultMap.getData());
        model.addAttribute("searchMap", searchMap);
        return "search";
    }

    @GetMapping(value = "/cart")
    public String cart() {
        return "cart";
    }

}
